package com.bnyy.medicalHousekeeper.moudle.health.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.SizeUtils;
import com.bnyy.health.bean.BloodPressureWeeklyHistory;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.base.BaseActivityImpl;
import com.bnyy.medicalHousekeeper.moudle.health.adapter.BloodPressureWeeklyHistoryAdapter;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.function.Consumer;

import butterknife.BindView;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/9/5
 **/
public class BloodPressureWeeklyHistoryActivity extends BaseActivityImpl {
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.iv_no_data)
    ImageView ivNoData;
    @BindView(R.id.tv_no_data)
    TextView tvNoData;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;

    public static void show(Context context, int userId) {
        Intent intent = new Intent(context, BloodPressureWeeklyHistoryActivity.class);
        intent.putExtra("userId", userId);
        context.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_recycler_view;
    }

    @Override
    public String getTitleStr() {
        return "历史周报";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        int userId = getIntent().getIntExtra("userId", -1);
        if (userId == -1) {
            finish();
        } else {
            int padding = SizeUtils.dp2px(12);
            recyclerView.setPadding(padding, padding, padding, padding);

            HashMap<String, Object> params = new HashMap<>();
            params.put("user_id", userId);
            requestManager.request(requestManager.mHealthDataRetrofitService.getBloodPressureWeeklyHistory(RequestManager.getJsonRequestBody(params)),
                    new BaseObserverImpl<ArrayList<BloodPressureWeeklyHistory>>(mContext) {
                        @Override
                        public void onSuccess(ArrayList<BloodPressureWeeklyHistory> bloodPressureWeeklyHistories) {
                            super.onSuccess(bloodPressureWeeklyHistories);

                            if (bloodPressureWeeklyHistories.size() == 0) {
                                llNoData.setVisibility(View.VISIBLE);
                            } else {
                                recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
                                BloodPressureWeeklyHistoryAdapter adapter = new BloodPressureWeeklyHistoryAdapter(mContext, new Consumer<BloodPressureWeeklyHistory.Detail>() {
                                    @Override
                                    public void accept(BloodPressureWeeklyHistory.Detail detail) {
                                        BloodPressureWeeklyActivity.show(mContext, userId, detail.getId());
                                    }
                                });
                                recyclerView.setAdapter(adapter);
                                adapter.refresh(bloodPressureWeeklyHistories);
                            }
                        }
                    });
        }
    }
}





























